import 'dart:io';

import 'package:flutter/material.dart';
import 'pages/peripherals_page.dart';
import 'pages/ble_func_list_page.dart';
import 'package:flutter_easy_permission/easy_permissions.dart';

void main() {
  runApp(MainPage());
}

class MainPage extends StatefulWidget {
  @override
  _MainPageState createState() => new _MainPageState();
}

class _MainPageState extends State<MainPage>
    with SingleTickerProviderStateMixin {
  late TabController _controller;
  static List<Permissions> permissions = [
    Permissions.ACCESS_FINE_LOCATION,
    Permissions.ACCESS_COARSE_LOCATION
  ];

  static List<PermissionGroup> permissionGroup = [
    PermissionGroup.Location,
    PermissionGroup.Bluetooth
  ];

  static String requestTitle = "APP需要蓝牙与定位授权";

  late FlutterEasyPermission _easyPermission;

  @override
  void initState() {
    super.initState();
    _controller = TabController(length: 2, vsync: this);

    if (Platform.isIOS) {
      permissions = [];
      permissionGroup = [PermissionGroup.Bluetooth];
      requestTitle = "APP需要蓝牙授权";
    }

    _easyPermission = FlutterEasyPermission()
      ..addPermissionCallback(onGranted: (requestCode, perms, perm) {
        debugPrint("android获得授权:$perms");
        debugPrint("iOS获得授权:$perm");
      }, onDenied: (requestCode, perms, perm, isPermanent) {
        if (isPermanent) {
          FlutterEasyPermission.showAppSettingsDialog(title: requestTitle);
        } else {
          debugPrint("android授权失败:$perms");
          debugPrint("iOS授权失败:$perm");
        }
      }, onSettingsReturned: () {
        FlutterEasyPermission.has(
            perms: permissions, permsGroup: permissionGroup)
            .then((value) => value
            ? debugPrint("已获得授权:$permissions")
            : debugPrint("未获得授权:$permissions"));
      });
    // FlutterEasyPermission.has(perms: permissions, permsGroup: permissionGroup)
    //     .then((value) => value ? debugPrint("已获得授权:$permissions") : debugPrint("未获得授权:$permissions"));
    FlutterEasyPermission.request(
        perms: permissions,
        permsGroup: permissionGroup,
        rationale: requestTitle);
  }

  @override
  void dispose() {
    _controller.dispose();
    _easyPermission.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.white,
        body: TabBarView(
          // physics: NeverScrollableScrollPhysics(),
          controller: _controller,
          children: <Widget>[
            PeripheralPage(),
            BleFuncListPage(),
          ],
        ),
        bottomNavigationBar: new Material(
          color: Colors.white,
          child: TabBar(
            controller: _controller,
            labelColor: Colors.blueAccent,
            unselectedLabelColor: Colors.black26,
            tabs: <Widget>[
              Tab(
                text: "设备",
                icon: Icon(Icons.devices),
                iconMargin: EdgeInsets.all(0),
              ),
              Tab(
                text: "功能列表",
                icon: Icon(Icons.list),
                iconMargin: EdgeInsets.all(0),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
